package com.ext.aic.apis.user;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.ext.aic.apis.ApiDefine;
import com.ext.aic.apis.ApiPaths;
import com.ext.aic.apis.BaseRequest;
import com.ext.aic.apis.RequestResponse;
import com.ext.aic.beans.params.RegisterParams;
import com.ext.aic.beans.response.ExceptionResponse;
import com.ext.aic.beans.response.RegisterResponse;
import com.ext.aic.beans.response.Response;
import com.eyinfo.android_pure_utils.events.Action0;
import com.eyinfo.android_pure_utils.utils.JsonUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.body.JsonRequestBody;

import lombok.Getter;
import lombok.Setter;

public class RegisterRequest extends BaseRequest<RegisterParams, Object> {

    @Getter
    @Setter
    private Action0 successCall;

    @Setter
    @Getter
    private Action0 reAuditCall;

    protected void onRegisterSuccess(RegisterResponse registerResponse) {

    }

    public void request(Context context, LifecycleOwner lifecycleOwner, RegisterParams params) {
        EasyHttp.post(lifecycleOwner)
                .api(new ApiDefine(ApiPaths.register))
                .body(new JsonRequestBody(JsonUtils.toJson(params)))
                .request(new RequestResponse<Response<RegisterResponse>>(context, false) {

                    @Override
                    protected void onSuccess(Response<RegisterResponse> response) {
                        if (response == null) {
                            return;
                        }
                        int statusCode = response.getCode();
                        if (statusCode == 200 || statusCode == 201) {
                            onRegisterSuccess(response.getData());
                        } else {
                            ExceptionResponse exceptionResponse = new ExceptionResponse();
                            exceptionResponse.setCode(statusCode);
                            exceptionResponse.setMessage(response.getMessage());
                            onFail(exceptionResponse, params, null);
                        }
                    }

                    @Override
                    public void onHttpFail(Throwable throwable) {
                        responseExceptionHandler(throwable, params, null);
                    }
                });

    }
}
